/**
 * *共用脚本方法
 */

/**
 * 去掉字符串前后的空格
 * 
 */
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim = function() {
	return this.replace(/(^\s*)/g, "");
}
String.prototype.rtrim = function() {
	return this.replace(/(\s*$)/g, "");
}

/**
 * 计算含中文的字符串的长度
 * 
 */
String.prototype.strLen = function() {
	var cArr = this.match(/[^\x00-\xff]/ig);
	return this.length + (cArr == null ? 0 : cArr.length);
}

/**
 * 消息弹出框的生命期时间, 单位毫秒
 * 
 * @type Number
 */
var LIFE_DEFAULT = 3000;
var LIFE_SHORT = 1000;
var LIFE_LONG = 5000;

var showMsg = function(msg, life) {

	if (null == life || undefined == life || NaN == life) {
		life = LIFE_DEFAULT;
	}

	$.jGrowl(msg, {
				sticky : false,
				life : life
			});
};

/**
 * 动态绑定事件
 * 
 * @param {}
 *            target
 * @param {}
 *            name
 * @param {}
 *            fn
 * @return {}
 */
var addEvent = function(target, name, fn) {
	var call = function() {
		fn.apply(target, arguments);
	};
	if (target.dom) {
		target = target.dom;
	}
	if (window.attachEvent) {
		target.attachEvent("on" + name, call);
	} else if (window.addEventListener) {
		target.addEventListener(name, call, false);
	} else {
		target["on" + name] = call;
	}
	return call;
}
// ---------------------------------------------------
// 将指定的number值添加到日期
// interval:s（秒）m（分）h（小时）d（天）w（周） q（季度） M（月）y (年)
// number:数值
// ---------------------------------------------------
Date.prototype.Add = function(interval, number) {
	switch (interval) {
		case 's' :
			return new Date(Date.parse(this) + (1000 * number));
		case 'm' :
			return new Date(Date.parse(this) + (60000 * number));
		case 'h' :
			return new Date(Date.parse(this) + (3600000 * number));
		case 'd' :
			return new Date(Date.parse(this) + (86400000 * number));
		case 'w' :
			return new Date(Date.parse(this) + ((86400000 * 7) * number));
		case 'M' :
			return new Date(this.getFullYear(), (this.getMonth()) + number,
					this.getDate());
		case 'q' :
			return new Date(this.getFullYear(), (this.getMonth()) + number * 3,
					this.getDate());
		case 'y' :
			return new Date((this.getFullYear() + number), this.getMonth(),
					this.getDate());
	}
}
Date.prototype.AddSecond = function(number) {
	return this.Add("s", number);
}
Date.prototype.AddMinute = function(number) {
	return this.Add("m", number);
}
Date.prototype.AddHour = function(number) {
	return this.Add("h", number);
}
Date.prototype.AddDay = function(number) {
	return this.Add("d", number);
}
Date.prototype.AddWeek = function(number) {
	return this.Add("w", number);
}
Date.prototype.AddMonth = function(number) {
	return this.Add("M", number);
}
Date.prototype.AddQuarter = function(number) {
	return this.Add("q", number);
}
Date.prototype.AddYear = function(number) {
	return this.Add("y", number);
}

/**
 * 取得当前时间，转换成yyyy-MM-dd hh:mi:ss格式
 */
var getNowDatetime = function(date) {

	// var date = new Date();
	var year = date.getFullYear().toString();
	var month = (date.getMonth() + 1).toString();
	var day = date.getDate().toString();

	var hour = date.getHours().toString();
	var minute = date.getMinutes().toString();
	var second = date.getSeconds().toString();
	var millSecond = date.getMilliseconds().toString();

	month = month.length == 1 ? "0" + month : month;
	day = day.length == 1 ? "0" + day : day;
	hour = hour.length == 1 ? "0" + hour : hour;
	minute = minute.length == 1 ? "0" + minute : minute;
	second = second.length == 1 ? "0" + second : second;

	var len = millSecond.length;
	for (var i = len; i < 3; i++) {
		millSecond += '0';
	}

	return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":"
			+ second + '.' + millSecond;
}
